package com.easy.sk.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.easy.common.bean.SeckillActivity;
import com.easy.common.bean.SeckillProduct;
import com.easy.common.exception.ServiceException;
import com.easy.common.service.ISeckillProductService;
import com.easy.common.util.SYS;
import com.easy.sk.system.mapper.SeckillActivityMapper;
import com.easy.common.service.ISeckillActivityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 秒杀活动表 服务实现类
 * </p>
 *
 * @author Easy
 * @since 2025-09-09
 */
@Service
@DubboService
public class SeckillActivityServiceImpl extends ServiceImpl<SeckillActivityMapper, SeckillActivity> implements ISeckillActivityService {
    @Autowired
    private ISeckillProductService seckillProductService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public boolean startPreheat(Integer id) {
        //参数id 秒杀活动id
        //redis中是否有这个活动，如果有，则已经开启
        String activity_redis_key = SYS.SK_PRE_ACTIVITY_KEY + id;
        if (redisTemplate.hasKey(activity_redis_key)) {
            throw new ServiceException("活动已经预热");
        }
        //获取数据库中的活动信息
        SeckillActivity activity = this.getById(id);
        if (activity == null) {
            throw new ServiceException("活动不存在");
        }
        if (activity.getStatus() != 0) {
            throw new ServiceException("活动状态异常");
        }
        LocalDateTime endTime = activity.getEndTime();
        //活动结束时间毫秒数
        Long endTimeMillis = endTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();

        LocalDateTime startTime = activity.getStartTime();
        //活动开始时间毫秒数
        Long startTimeMillis = startTime.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
        //当前时间毫秒数
        Long currentTimeMillis = System.currentTimeMillis();
        if (currentTimeMillis > endTimeMillis) {
            throw new ServiceException("活动已结束");
        }
        //修改数据库中秒杀活动的状态
        activity.setStatus((byte) 1);
        this.updateById(activity);
        //开始预热操作
        //1.将活动信息保存到redis中
        Map activityMap = new HashMap();
        activityMap.put(SYS.SK_ACTIVITY_STATUS_HASH_KEY, 1);
        activityMap.put(SYS.SK_ACTIVITY_START_TIME_KEY, startTimeMillis);
        activityMap.put(SYS.SK_ACTIVITY_END_TIME_KEY, endTimeMillis);
        redisTemplate.opsForHash().putAll(activity_redis_key, activityMap);

        //2.将秒杀商品信息保存到redis中
        //获取数据库中的秒杀商品信息
        QueryWrapper<SeckillProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SeckillProduct::getActivityId, id);//sql where activity_id = ?  #{id}
        List<SeckillProduct> seckillProducts = seckillProductService.list(queryWrapper);
        for (SeckillProduct seckillProduct : seckillProducts) {
            String product_redis_key = SYS.SK_PRE_PRODUCT_KEY + seckillProduct.getId();//存入redis的秒杀商品的key
            int sk_stock = seckillProduct.getStock();//秒杀库存
            redisTemplate.opsForValue().set(product_redis_key, sk_stock);
        }
        return true;
    }
}
